package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlertDataServiceImpl implements AlertDataService {

    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询报警数据
     *
     * @param alertDataPageQueryDto
     * @return
     */
    @Override
    public PageResponse<AlertDataVo> queryPageQueryAlertData(AlertDataPageQueryDto alertDataPageQueryDto) {
        PageHelper.startPage(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        //处理日期
        if (alertDataPageQueryDto.getStartTime() != null && alertDataPageQueryDto.getEndTime() != null) {
            alertDataPageQueryDto.setStartDateTime(LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime()));
            alertDataPageQueryDto.setEndDateTime(LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime()));
        }
        Page<AlertDataVo> page = alertDataMapper.selectPageQueryAlertData(alertDataPageQueryDto);
        return PageResponse.of(page, AlertDataVo.class);
    }

    /**
     * 处理设备报警数据
     *
     * @param alertData
     */
    @Override
    public void handleAlertDataById(AlertData alertData, Long id) {
        alertData.setId(id);
        //获取当前处理人id
        Long mgtUserId = UserThreadLocal.getMgtUserId();
        //根据id查询当前处理人
        User user = userMapper.selectUserById(mgtUserId);
        alertData.setProcessorId(mgtUserId);
        alertData.setProcessorName(user.getRealName());
        alertDataMapper.updateAlertDataById(alertData);
    }
}
